<?php
namespace app\admin\controller\media;


use app\common\controller\AdminController;
use think\App;
use think\facade\Db;
use think\facade\View;

/**
 * Class Articles
 * @package app\admin\controller\media
 * 平台文章
 */
class Articles extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\media\Articles');
        $this->order = ['sort'=>'asc','id'=>'desc'];
    }

    public function get_where($array = []){
        $arr[] = ['type','=',1];
        if(isset($array['title']) && !empty($array['title'])){
            $arr[] = ['title','like','%'.$array['title'].'%'];
        }
        return $arr;
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if(empty($post['image'])){
                $this->error('请上传图片图片！');
            }
            if(count($post['image'])!=1 && count($post['image'])!=3){
                $this->error('图片请上传三张或一张！');
            }
            $post['thumb'] = $post['image'][0];
            $post['type'] = 1;
            $post['status'] = 1;
            // 启动事务
            Db::startTrans();
            try {
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }

                $this->validate($post, $rule);

                $post['app_id'] = $this->model->getApp();
                $save = $this->model->save($post);

                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->find($id);

        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            if(empty($post['image'])){
                $this->error('请上传图片图片！');
            }
            if(count($post['image'])!=1 && count($post['image'])!=3){
                $this->error('图片请上传三张或一张！');
            }
            try {
                $post['thumb'] = $post['image'][0];
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }
                $this->validate($post, $rule);

                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error($e->getMessage());
        }
        View::assign('row', $row);
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view('', compact('row'));
    }

    public function action(){

        $param= $this->request->param();

        try{
            $row = $this->model->where('id',$param['id'])->find();
            $row->save($param);

        }catch (\Exception $exception){
            return json(['code'=>1,'msg'=>$exception->getMessage()]);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $advertmodel = app('app\common\model\banner\Advert');//广告
        $row = $this->model->whereIn('id', $id)->select();
        empty($row) && $this->error('数据不存在');
        $advert = $advertmodel->where(['type'=>2,'bind_id'=>$id])->find();
        if(!empty($advert)){$this->error('此资讯文章已绑定广告，请解绑后在删除');}
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }
}